<?php
/* By QQ:834633039 */
global $_W,$_GPC;

$weid = $_W['uniacid'];

$op = empty($_GPC['op'])? 'list':$_GPC['op'];

if($op=='list'){
	
	$listid = intval($_GPC['listid']);
	
	if(empty($listid)){
		message('参数错误！');
	}
	$list = pdo_fetch("SELECT * FROM ".tablename("meepo_online_list")." WHERE weid = :weid AND id = :id", array(':weid'=>$weid,':id'=>$listid));
	if(empty($list)){
		message('直播参数错误！');
	}
	
	$pindex = max(1, intval($_GPC['page']));

	$psize = 10;

	$categorys = pdo_fetchall("SELECT * FROM ".tablename("meepo_online_adv_list")." WHERE weid=:weid AND listid=:listid ORDER BY displayorder ASC,createtime DESC  LIMIT ".($pindex - 1) * $psize.",{$psize}",array(':weid'=>$weid,':listid'=>$listid));

	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename("meepo_online_adv_list") . " WHERE weid=:weid AND listid=:listid", array(':weid'=>$weid,':listid'=>$listid));

	$pager = pagination($total, $pindex, $psize);

}elseif($op=='post'){

	load()->func('tpl');
	
	$listid = intval($_GPC['listid']);
	$id = intval($_GPC['id']);

	$adv = pdo_fetch("SELECT * FROM ".tablename("meepo_online_adv_list")." WHERE weid=:weid AND id=:id AND listid = :listid",array(':weid'=>$weid,':id'=>$id,':listid'=>$listid));

	if(empty($adv)){

		$adv = array(

			'isshow'=>1,

		);

	}

	if (checksubmit('submit')) {

		$data = array();

		$data['weid'] = $weid;
		
		$data['listid'] = $listid;
		
		$data['title'] = $_GPC['title'];

		$data['isshow'] = intval($_GPC['isshow']);

		$data['img'] = $_GPC['img'];

		$data['url'] = $_GPC['url'];

		$data['displayorder'] = intval($_GPC['displayorder']);

		if(!empty($id)){

			pdo_update("meepo_online_adv_list",$data,array('id'=>$id,'weid'=>$weid,'listid'=>$listid));

			message('编辑成功',$this->createWebUrl('adv_list',array("listid" => $listid)),'success');

		}else{

			$data['createtime'] = time();

			pdo_insert("meepo_online_adv_list",$data);

			message('新增成功',$this->createWebUrl('adv_list',array("listid" => $listid)),'success');

		}

	}

}elseif($op=='del'){
	
	$listid = intval($_GPC['listid']);
	$id = intval($_GPC['id']);

	$category = pdo_fetch("SELECT * FROM ".tablename("meepo_online_adv_list")." WHERE weid=:weid AND id=:id AND listid=:listid",array(':weid'=>$weid,':id'=>$id,':listid'=>$listid));

	if(empty($category)){

		message('此项不存在或是已经被删除',referer());

	}else{

		pdo_delete("meepo_online_adv_list",array('id'=>$id,'weid'=>$weid,'listid'=>$listid));

	}

	message('删除成功',referer());

}

include $this->template('adv_list',array("listid" => $listid));